package oracle.springbootxm.service.impl;

import oracle.springbootxm.dao.menudao.MenuMapper;
import oracle.springbootxm.pojo.MenuInfo;
import oracle.springbootxm.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper mapper;

    @Override
    public MenuInfo queryMenuInfo() {
        List<MenuInfo> menuInfos = mapper.queryMenuInfo();
        MenuInfo root = new MenuInfo();
        root.setMenuid(10);
        sortMenuTree(root, menuInfos);
        return root;
    }


    public MenuInfo sortMenuTree(MenuInfo root, List<MenuInfo> menuInfos) {
        for (int i = 0; i < menuInfos.size(); i++) {
            MenuInfo element = menuInfos.get(i);
            if (element.getParentid() == root.getMenuid()) {
                root.getChildmenu().add(element);
                sortMenuTree(element, menuInfos);
            }
        }
        return root;
    }
}
